<script setup lang="ts">
import type { Page, GroupTable } from '@/types'
import { dateFormat, getFormatDictMap } from '@/util/dict'
import { defineAsyncComponent, onMounted, ref } from 'vue'
import { Search, Refresh } from '@/util/icons'
import Pagination from '@/components/Pagination.vue'
import { getAllGroup } from './api'
import AuthButton from '@/components/AuthButton.vue'
import AuthLink from '@/components/AuthLink.vue'
// 菜单名称
const pageN = '用户组'
const page = ref<Page<GroupTable>>({
  num: 1,
  size: 10,
  rows: [],
  count: 0
})
/* 获取对应的字典信息 */
const tableDict = ref<Map<string, string>>()
const statusDict = ref<Map<string, string>>()

// 页面加载
const loading = ref<boolean>(false)
// 获取页面数据
function getPageData() {
  loading.value = true
  getAllGroup(page.value, searchForm.value)
    .then((tabs) => {
      page.value = tabs.data
    })
    .finally(() => {
      setTimeout(() => {
        loading.value = false
      }, 500)
    })
}
onMounted(() => {
  getFormatDictMap('sys_group', 'head').then((d) => {
    tableDict.value = d
  })
  getFormatDictMap('sys_role_status', 'val').then((d) => {
    statusDict.value = d
  })
  getPageData()
})

/* 搜索 */
/* 是否展示搜索栏 */
const showSearchBar = ref<boolean>(true)
const searchForm = ref<GroupTable>({
  name: '',
  status: ''
})

// 重置搜索框
function reSearch() {
  searchForm.value = {
    name: '',
    status: ''
  }
  getPageData()
}
/* 对话框控制 */
const dialogVisible = ref<boolean>(false)

/* 选择器 */
const selectData = ref<GroupTable[]>([])
function onSelect(data: GroupTable[]) {
  selectData.value = data
}
const delData = ref<GroupTable[]>([])
/* 控制器 */
const controlType = ref<'none' | 'del' | 'add' | 'edit' | 'editAuth' | 'editUser'>('none')
const dialogWidth = ref<string>('auto')
const dialogTitle = ref('')
// 删除控制
const Del = defineAsyncComponent(() => import('./Del.vue'))
function del(rows: GroupTable[]) {
  controlType.value = 'del'
  dialogWidth.value = '300'
  dialogTitle.value = `你确定要删除以下${pageN}吗?`
  delData.value = rows
  dialogVisible.value = true
}
// 添加控制
const Add = defineAsyncComponent(() => import('./Add.vue'))
function add() {
  controlType.value = 'add'
  dialogWidth.value = '400'
  dialogTitle.value = `添加${pageN}`
  dialogVisible.value = true
}

//编辑控制
const Edit = defineAsyncComponent(() => import('./Edit.vue'))
const EditData = ref<GroupTable>()
function edit(d: GroupTable) {
  EditData.value = d
  controlType.value = 'edit'
  dialogWidth.value = '400'
  dialogTitle.value = `修改${pageN}`
  dialogVisible.value = true
}

//角色接口权限配置
const UpdateAuth = defineAsyncComponent(() => import('./UpdateAuth.vue'))
const editId = ref(0)
function editAuth(d: number) {
  editId.value = d
  controlType.value = 'editAuth'
  dialogWidth.value = '80%'
  dialogTitle.value = `配置${pageN}权限`
  dialogVisible.value = true
}

//菜单权限配置
const UpdateUser = defineAsyncComponent(() => import('./UpdateUser.vue'))
function editUser(d: number) {
  editId.value = d
  controlType.value = 'editUser'
  dialogWidth.value = '80%'
  dialogTitle.value = `配置${pageN}用户`
  dialogVisible.value = true
}
</script>

<template>
  <div class="page-root">
    <div class="control">
      <div class="search" v-show="showSearchBar">
        <el-form :inline="true">
          <el-form-item :label="tableDict?.get('name') || 'name'">
            <el-input
              v-model="searchForm.name"
              :placeholder="'请输入' + tableDict?.get('name') || 'name'"
            ></el-input>
          </el-form-item>
          <el-form-item :label="tableDict?.get('status') || 'status'">
            <el-select
              v-model="searchForm.status"
              :placeholder="'请选择' + tableDict?.get('status') || 'status'"
            >
              <el-option label="无" value="" />
              <el-option :label="statusDict?.get('1') || '1'" value="1" />
              <el-option :label="statusDict?.get('0') || '0'" value="0" />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" :icon="Search" @click="getPageData">搜索</el-button>
            <el-button :icon="Refresh" @click="reSearch">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
      <div class="ctl-bts">
        <span class="ctl-l">
          <AuthButton @click="add" auth-tag="sys:group:add"></AuthButton>
          <AuthButton
            @click="edit(selectData[0])"
            :disabled="selectData.length != 1"
            auth-tag="sys:group:edit"
          ></AuthButton>
          <AuthButton
            @click="del(selectData)"
            :disabled="selectData.length == 0"
            auth-tag="sys:group:del"
          />
        </span>
        <span class="ctl-r">
          <el-button :icon="Search" circle @click="showSearchBar = !showSearchBar"></el-button>
          <el-button :icon="Refresh" @click="getPageData" circle></el-button>
        </span>
      </div>
    </div>
    <div class="ctx">
      <el-table
        v-loading="loading"
        table-layout="auto"
        :stripe="true"
        :data="page.rows"
        height="100%"
        :flexible="true"
        @selection-change="onSelect"
      >
        <el-table-column type="selection" width="50" />
        <el-table-column prop="name" align="center">
          <template #header>
            {{ tableDict?.get('name') || 'name' }}
          </template>
        </el-table-column>
        <el-table-column prop="status" align="center">
          <template #header>
            {{ tableDict?.get('status') || 'status' }}
          </template>
          <template #default="{ row }">
            <el-tag :type="row['status'] === '1' ? 'success' : 'danger'">{{
              statusDict?.get(row['status']) || row['status']
            }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" align="center">
          <template #header>
            {{ tableDict?.get('createTime') || 'createTime' }}
          </template>
          <template #default="{ row }">
            <span>{{ dateFormat(row['createTime']) }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="updateTime" align="center">
          <template #header>
            {{ tableDict?.get('updateTime') || 'updateTime' }}
          </template>
          <template #default="{ row }">
            <span>{{ dateFormat(row['updateTime']) }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="description" align="center">
          <template #header>
            {{ tableDict?.get('description') || 'description' }}
          </template>
        </el-table-column>
        <el-table-column align="center" width="400">
          <template #header> 操作 </template>
          <template #default="{ row }">
            <div class="buttons">
              <AuthLink auth-tag="sys:group:user" @click="editUser(row.id)"></AuthLink>
              <AuthLink auth-tag="sys:group:auth" @click="editAuth(row.id)"></AuthLink>
              <AuthLink auth-tag="sys:group:edit" @click="edit(row)"></AuthLink>
              <AuthLink auth-tag="sys:group:del" @click="del([row])"></AuthLink>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <Pagination :page="page" :get-page-data="getPageData" />
    </div>
    <el-dialog
      @closed="controlType = 'none'"
      top="5%"
      v-model="dialogVisible"
      :title="dialogTitle"
      :width="dialogWidth"
    >
      <Del
        v-if="controlType == 'del'"
        :refresh="getPageData"
        :table-dict="tableDict!"
        :rows="delData"
        @c-dlg="dialogVisible = false"
      />
      <Add
        v-else-if="controlType == 'add'"
        :refresh="getPageData"
        :d-map="tableDict!"
        @c-dlg="dialogVisible = false"
      />
      <Edit
        v-else-if="controlType == 'edit'"
        :refresh="getPageData"
        :d-map="tableDict!"
        :edit-data="EditData!"
        @c-dlg="dialogVisible = false"
      />
      <UpdateAuth
        v-else-if="controlType == 'editAuth'"
        :edit-id="editId"
        @c-dlg="dialogVisible = false"
      />
      <UpdateUser v-else-if="controlType == 'editUser'" :group="editId" />
    </el-dialog>
  </div>
</template>
<style scoped lang="less">
@import '@/assets/page_bgl.less';
</style>
